package user;

import cake.UserDao;
import cake.UserDaoImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdmLoginServlet", value = "/AdmLoginServlet")
public class AdmLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("password");
        UserDao ud = new UserDaoImpl();
        if(ud.admlogin(name,pwd)){
            /*如果要获取request存放的参数则使用请求转发，路径加不加/都行 */
            System.out.println("用户" + name + "登录成功");
            /*req.setAttribute向request域中添加名为msg对象object为string类型的信息*/
            request.setAttribute("msg", name );
            request.getRequestDispatcher("showAll.jsp").forward(request,response);
        }else {
            /*如果不获取参数则使用重定向到错误页面*/
            response.sendRedirect("error.jsp");
        }
    }
}
